package com.joel.crm.commandParseImpl;

import java.util.Scanner;

import com.joel.client.Inital;
import com.joel.crm.model.DoubleLinkedlist;
import com.joel.crm.util.UserHelp;
import com.joel.functionsImpl.FunctionImpl;
import com.joel.interfaces.CommandParse;

public class CommandParseImpl implements CommandParse {
	DoubleLinkedlist doubleLinkedlist;
	FunctionImpl functionImpl;
	public CommandParseImpl(){
		doubleLinkedlist = Inital.inital();
		functionImpl = new FunctionImpl(doubleLinkedlist);
	}

	@Override
	public void cmdParse() {
		Scanner scanner = new Scanner(System.in);
		try {
			
			while (scanner.hasNext()) {
				String command = scanner.nextLine();
				if (command.length() == 1) {
					switch (command) {
					case "H":
						UserHelp.getHelpInfo();
						break;
					case "L":
						doubleLinkedlist.show();
						System.out.println("L is the command to check information, you can also check the message by id like:\"L id:1\"");
						break;
					case "A":
						System.out.println("A is the command to add information like A id:11,name:Paul,sex:male,birth:2000-1-6,address:China,phone:565656");
						break;
					case "U":
						System.out.println("U is the command to alter information like input \"U id:1,name:Durken\"can change the name of customer whoes id is 1");
						break;
					case "D":
						System.out.println("D is the command to delete, you can input \"D id:1\"to delete the info of customer whose id is 1");
						break;
					case "Q":
						System.out.println("Good Bye");
						scanner.close();
						break;
					case "O":
						System.out.println("O is the command to sort by id, eg:O id");
						break;
					default:
						System.out.println("can not resolve the command, please try reinput");
						break;
					}
				} else if (command.length() == 2) {
					System.out.println("can resolve the command, please try to use space to split");
				} else if (command.equals("H A")) {
					UserHelp.getHelpHA();
				} else if (command.equals("H U")) {
					UserHelp.getHelpHU();
				} else if (command.equals("H Q")) {
					UserHelp.getHelpHQ();
				} else if (command.equals("H D")) {
					UserHelp.getHelpHD();
				} else if (command.equals("H L")) {
					UserHelp.getHelpHL();
				}
				else if (command.equals("H O")) {
					UserHelp.getHelpHO();
				}
				else {
					String[] com = new String[2];
					com = command.split(" ");
					String b = com[1];
					String com2[] = b.split(",");
					switch (com[0]) {
					case "H":
						UserHelp.getHelpInfo();
						break;
					case "L":
						functionImpl.list(com2);
						break;
					case "A":
						System.out.println("add succeed! please check by command L");
						functionImpl.add(com2);
						break;
					case "U":
						functionImpl.update(com2);
						break;
					case "D":
						functionImpl.delete(com2);
						break;
					case "O":
						System.out.println("the sort result by id is: ");
						functionImpl.sortById();
						break;
					case "Q":
						System.out.println("See you later");
						scanner.close();
						break;
					default:
						System.out.println("can not resolve command,please input again");
						break;
					}
				}
			}
		} catch (IllegalStateException e) {
			System.out.println("welcome back next time");
		} 
		  catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("sorry! system can not resolve command,please start-up again");
		}
		finally {
			scanner.close();
		}

	}

}
